<?php
class BlogTagsController extends AppController
{
    public $name = 'BlogTags';
    public function index()
    {
        $this->pageTitle = __l('Update Tags');
        $blogTag = $this->BlogTag->find('all', array(
            'recursive' => 1,
            'contain' => array(
                'Blog' => array(
                    'fields' => array(
                        'id'
                    )
                )
            )
        ));
        $tag_arr = array();
        foreach($blogTag as $blogTag) {
            $tag_arr[$blogTag['BlogTag']['slug']] = count($blogTag['Blog']);
            $tag_name_arr[$blogTag['BlogTag']['slug']] = $blogTag['BlogTag']['name'];
        }
        $this->set('tag_arr', $tag_arr);
        $this->set('tag_name_arr', $tag_name_arr);
    }
    public function view($id = null)
    {
        $this->pageTitle = __l('Update Tag');
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        $blogTag = $this->BlogTag->find('first', array(
            'conditions' => array(
                'BlogTag.id = ' => $id
            ) ,
            'fields' => array(
                'BlogTag.id',
                'BlogTag.created',
                'BlogTag.modified',
                'BlogTag.name',
                'Blog.id',
                'Blog.created',
                'Blog.modified',
                'Blog.title',
                'Blog.content',
                'Blog.slug',
                'Blog.blog_comment_count',
            ) ,
            'recursive' => 0,
        ));
        if (empty($blogTag)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        $this->pageTitle.= ' - ' . $blogTag['BlogTag']['name'];
        $this->set('blogTag', $blogTag);
    }
    public function add()
    {
        $this->pageTitle = __l('Add Update Tag');
        if (!empty($this->request->data)) {
            $this->BlogTag->create();
            if ($this->BlogTag->save($this->request->data)) {
                $this->Session->setFlash(sprintf(__l('"%s" Update Tag has been added') , $this->request->data['BlogTag']['name']) , 'default', null, 'success');
                $this->redirect(array(
                    'action' => 'index'
                ));
            } else {
                $this->Session->setFlash(sprintf(__l('"%s" Update Tag could not be added. Please, try again.') , $this->request->data['BlogTag']['name']) , 'default', null, 'error');
            }
        }
        $blogs = $this->BlogTag->Blog->find('list');
        $this->set(compact('blogs'));
    }
    public function edit($id = null)
    {
        $this->pageTitle = __l('Edit Update Tag');
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        if (!empty($this->request->data)) {
            if ($this->BlogTag->save($this->request->data)) {
                $this->Session->setFlash(sprintf(__l('"%s" Update Tag has been updated') , $this->request->data['BlogTag']['name']) , 'default', null, 'success');
            } else {
                $this->Session->setFlash(sprintf(__l('"%s" Update Tag could not be updated. Please, try again.') , $this->request->data['BlogTag']['name']) , 'default', null, 'error');
            }
        } else {
            $this->request->data = $this->BlogTag->read(null, $id);
            if (empty($this->request->data)) {
                throw new NotFoundException(__l('Invalid request'));
            }
        }
        $this->pageTitle.= ' - ' . $this->request->data['BlogTag']['name'];
        $blogs = $this->BlogTag->Blog->find('list');
        $this->set(compact('blogs'));
    }
    public function delete($id = null)
    {
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        if ($this->BlogTag->delete($id)) {
            $this->Session->setFlash(__l('Update Tag deleted') , 'default', null, 'success');
            $this->redirect(array(
                'action' => 'index'
            ));
        } else {
            throw new NotFoundException(__l('Invalid request'));
        }
    }
    public function admin_index()
    {
        $this->pageTitle = __l('Update Tags');
        $this->BlogTag->recursive = 0;
        $this->set('blogTags', $this->paginate());
    }
    public function admin_view($id = null)
    {
        $this->pageTitle = __l('Update Tag');
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        $blogTag = $this->BlogTag->find('first', array(
            'conditions' => array(
                'BlogTag.id = ' => $id
            ) ,
            'fields' => array(
                'BlogTag.id',
                'BlogTag.created',
                'BlogTag.modified',
                'BlogTag.name',
                'Blog.id',
                'Blog.created',
                'Blog.modified',
                'Blog.title',
                'Blog.content',
                'Blog.slug',
                'Blog.blog_comment_count',
            ) ,
            'recursive' => 0,
        ));
        if (empty($blogTag)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        $this->pageTitle.= ' - ' . $blogTag['BlogTag']['name'];
        $this->set('blogTag', $blogTag);
    }
    public function admin_add()
    {
        $this->pageTitle = __l('Add Update Tag');
        if (!empty($this->request->data)) {
            $this->BlogTag->create();
            if ($this->BlogTag->save($this->request->data)) {
                $this->Session->setFlash(sprintf(__l('"%s" Update Tag has been added') , $this->request->data['BlogTag']['name']) , 'default', null, 'success');
                $this->redirect(array(
                    'action' => 'index'
                ));
            } else {
                $this->Session->setFlash(sprintf(__l('"%s" Update Tag could not be added. Please, try again.') , $this->request->data['BlogTag']['name']) , 'default', null, 'error');
            }
        }
        $blogs = $this->BlogTag->Blog->find('list');
        $this->set(compact('blogs'));
    }
    public function admin_edit($id = null)
    {
        $this->pageTitle = __l('Edit Update Tag');
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        if (!empty($this->request->data)) {
            if ($this->BlogTag->save($this->request->data)) {
                $this->Session->setFlash(sprintf(__l('"%s" Update Tag has been updated') , $this->request->data['BlogTag']['name']) , 'default', null, 'success');
            } else {
                $this->Session->setFlash(sprintf(__l('"%s" Update Tag could not be updated. Please, try again.') , $this->request->data['BlogTag']['name']) , 'default', null, 'error');
            }
        } else {
            $this->request->data = $this->BlogTag->read(null, $id);
            if (empty($this->request->data)) {
                throw new NotFoundException(__l('Invalid request'));
            }
        }
        $this->pageTitle.= ' - ' . $this->request->data['BlogTag']['name'];
        $blogs = $this->BlogTag->Blog->find('list');
        $this->set(compact('blogs'));
    }
    public function admin_delete($id = null)
    {
        if (is_null($id)) {
            throw new NotFoundException(__l('Invalid request'));
        }
        if ($this->BlogTag->delete($id)) {
            $this->Session->setFlash(__l('Update Tag deleted') , 'default', null, 'success');
            $this->redirect(array(
                'action' => 'index'
            ));
        } else {
            throw new NotFoundException(__l('Invalid request'));
        }
    }
}
?>